package com.hengxinguotong.hxgtwygsp.animator;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.text.TextUtils;

/**
 * TODO
 *
 * @author hjz
 * @version V1.0.0
 * @since 2018/10/31 15:08
 */

public class TranslationBuilder implements AnimatorBuilder
{
    private ObjectAnimator objectAnimator;
    private Object target;
    private String propertyName;
    private float[] values;
    private long duration = 1000;
    private int repeatMode = ValueAnimator.RESTART;
    private int repeatCount = 0;
    private Animator.AnimatorListener listener;

    @Override
    public AnimatorBuilder buildTarget(Object target)
    {
        this.target = target;
        return this;
    }

    @Override
    public AnimatorBuilder buildPropertyName(String propertyName)
    {
        this.propertyName = propertyName;
        return this;
    }

    @Override
    public AnimatorBuilder buildValues(float... values)
    {
        this.values = values;
        return this;
    }

    @Override
    public AnimatorBuilder buildDuration(long duration)
    {
        this.duration = duration;
        return this;
    }

    @Override
    public AnimatorBuilder buildRepeatMode(int value)
    {
        this.repeatMode = value;
        return this;
    }

    @Override
    public AnimatorBuilder buildRepeatCount(int value)
    {
        this.repeatCount = value;
        return this;
    }

    @Override
    public AnimatorBuilder buildListener(Animator.AnimatorListener listener)
    {
        this.listener = listener;
        return this;
    }

    @Override
    public ObjectAnimator create()
    {
        if (target == null)
        {
            return null;
        }
        if (TextUtils.isEmpty(propertyName) || !propertyName.startsWith("translation"))
        {
            return null;
        }
        if (values == null || values.length < 2)
        {
            return null;
        }
        objectAnimator = ObjectAnimator.ofFloat(target, propertyName, values).setDuration(duration);
        objectAnimator.setRepeatMode(repeatMode);
        objectAnimator.setRepeatCount(repeatCount);
        if (listener != null)
        {
            objectAnimator.addListener(listener);
        }
        return objectAnimator;
    }
}
